package com.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * Collections的基本方法介绍
 * 
 * @author Administrator
 *
 */
public class Utilities {

	static List<String> list = Arrays.asList("one two three Four five six one"
			.split(" "));

	public static void main(String[] args) {
		System.out.println(list);

		// singletonList产生不可变的List<T>，他们只包含基于所给定参数的内容而形成的单一项
		// disjoint当两个集合没有任何相同的元素时，返回true
		System.out
				.println("'list' disjoint(Four)?: "
						+ Collections.disjoint(list,
								Collections.singletonList("Four")));

		// 返回list中的最大或最小的元素，采用内置的自然比较法或Comparator比较法
		System.out.println(Collections.max(list));
		System.out.println(Collections.min(list));
		System.out.println("max w/ comparator: "
				+ Collections.max(list, String.CASE_INSENSITIVE_ORDER));
		System.out.println("min w/ comparator: "
				+ Collections.min(list, String.CASE_INSENSITIVE_ORDER));

		List<String> sublist = Arrays.asList("Four five six".split(" "));
		// indexOfSubList返回sublist在list中第一次出现的位置，找不到返回时返回-1
		System.out.println("indexOfSubList: "
				+ Collections.indexOfSubList(list, sublist));
		System.out.println("lastIndexOfSubList: "
				+ Collections.lastIndexOfSubList(list, sublist));

		Collections.replaceAll(list, "one", "Yo");
		System.out.println(list);

		// 逆转所有元素次序
		Collections.reverse(list);
		System.out.println(list);

		// 所有元素向后移动3个位置，将末尾的元素循环到前面来
		Collections.rotate(list, 3);
		System.out.println(list);

		// 将source中的元素复制到list
		List<String> source = Arrays.asList("in the matrix".split(" "));
		Collections.copy(list, source);
		System.out.println(list);

		// 交换list中位置i与位置j的元素
		Collections.swap(list, 0, list.size() - 1);
		System.out.println(list);

		// 重新排序
		Collections.shuffle(list, new Random(47));
		System.out.println(list);

		// 用对象"pop"，替换list中的所有元素
		Collections.fill(list, "pop");
		System.out.println(list);

		// 返回list中等于"pop"的元素个数
		System.out.println("frequency of 'pop': "
				+ Collections.frequency(list, "pop"));

		// 返回大小为3的list，此list不可改变，其中的引用都指向"snap"
		List<String> dups = Collections.nCopies(3, "snap");
		System.out.println(dups);
		System.out.println("'list' disjoint 'dups'?: "
				+ Collections.disjoint(list, dups));
	}

}
